package org.lex.dbq.model.jdbc.object;

public class TreeUtils {
	public static ObjectNode getAncestor(ObjectNode node, ObjectType type) {
		ObjectNode parent = (ObjectNode) node.getParent();
		while (null != parent) {
			if (type == parent.getType()) {
				break;
			}
			parent = (ObjectNode) parent.getParent();
		}
		return parent;
	}

	public static String getCatalogName(ObjectNode table) {
		String catalogName = null;
		ObjectNode catalog = getAncestor(table, ObjectType.CATALOG);
		if (null != catalog) {
			catalogName = catalog.getData();
		}
		return catalogName;
	}

	public static String getSchemaName(ObjectNode table) {
		String schemaName = null;
		ObjectNode schema = getAncestor(table, ObjectType.SCHEMA);
		if (null != schema) {
			schemaName = schema.getData();
		}
		return schemaName;
	}
}
